package cn.edu.ctbu.lddweb.controller;

import cn.edu.ctbu.lddweb.pojo.Score;
import cn.edu.ctbu.lddweb.pojo.Semester;
import cn.edu.ctbu.lddweb.pojo.Student;
import cn.edu.ctbu.lddweb.service.ScoreService;
import cn.edu.ctbu.lddweb.service.SemesterService;
import cn.edu.ctbu.lddweb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 成绩管理Controller（超级管理员）
 */
@Controller
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private SemesterService semesterService;

    //后台管理系统
    @RequestMapping("/score/index")
    public String index(Model model, ModelMap map,Score score){
        List<Score> list = scoreService.selectList(score);
        model.addAttribute("score",list);

        //学期下拉列表
        Semester semester = new Semester();
        List<Semester> list1 = semesterService.selectList(semester);
        model.addAttribute("semester",list1);

        //学生下拉列表
        Student student = new Student();
        List<Student> list2 = studentService.selectList(student);
        model.addAttribute("student",list2);
        return "/score/index";
    }

    //搜索/重置
    @RequestMapping("/selectScore")
    public String selectScore(@RequestParam(value = "studentName",required = false) String studentName, @RequestParam(value = "semesterName",required = false) String semesterName, Model model){
        Score score = new Score();
        score.setStudentName(studentName);
        score.setSemesterName(semesterName);
        List<Score> list = scoreService.selectList(score);
        model.addAttribute("score",list);

        //学期下拉列表
        Semester semester = new Semester();
        List<Semester> list1 = semesterService.selectList(semester);
        model.addAttribute("semester",list1);

        //学生下拉列表
        Student student = new Student();
        List<Student> list2 = studentService.selectList(student);
        model.addAttribute("student",list2);
        return "/score/index";
    }

    //新增页面
    @RequestMapping("/score/insert")
    public String insert(Model model){
        //学期下拉列表
        Semester semester = new Semester();
        List<Semester> list1 = semesterService.selectList(semester);
        model.addAttribute("semester",list1);

        //学生下拉列表
        Student student = new Student();
        List<Student> list2 = studentService.selectList(student);
        model.addAttribute("student",list2);
        return "/score/insert";
    }

    //新增操作
    @RequestMapping("/insertScore")
    public String insertScore(Score score){
        //判断该学生成绩是否存在（相同的学期下不能重复一门课的成绩）
        List<Score> list = scoreService.selectList(score);
        if(list.size()!=0){
            return "error111";
        }
        scoreService.insertScore(score);
        return "/score/index";
    }


    //修改
    @RequestMapping("/score/edit")
    public String edit(int id,Model model){
        Score score = scoreService.findID(id);
        model.addAttribute("score",score);

        //学期下拉列表
        Semester semester = new Semester();
        List<Semester> list1 = semesterService.selectList(semester);
        model.addAttribute("semester",list1);

        //学生下拉列表
        Student student = new Student();
        List<Student> list2 = studentService.selectList(student);
        model.addAttribute("student",list2);
        return "/score/edit";
    }

    //修改操作
    @RequestMapping("/updateScore")
    public String updateScore(Score score){
        Boolean boo = scoreService.checkScoreUnique(score);
        if(boo){
            return "error111";
        }
        scoreService.updateScore(score);
        return "/score/index";
    }

    //删除
    @RequestMapping("/deleteScore")
    public String deleteScore(int id){
        scoreService.deleteScore(String.valueOf(id));
        return "/score/index";
    }
}
